﻿// debugger;
// Trim 函数
String.prototype.Trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
// LTrim
String.prototype.LTrim = function () {
    return this.replace(/(^\s*)/g, "");
}
// RTrim
String.prototype.RTrim = function () {
    return this.replace(/(\s*$)/g, "");
}

// RTrim
String.prototype.Replace = function (beforeValue, afterValue) {
    return eval("this.replace(/" + beforeValue + "/g,\"" + afterValue + "\")");
}

function CheckAll(gridViewID) {
    var GridView1 = document.getElementById(gridViewID);
    for (i = 1; i < GridView1.rows.length; i++) {
        GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked =
        !GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked;
    }
}
// 限制只能输入电话号码 示例：<input type="text" id="txtTel" onkeydown="telKeyDown();">
function telKeyDown() {
    var k = window.event.keyCode;
    if ((k == 46) || (k == 8) || (k >= 48 && k <= 57) || (k >= 96 && k <= 105) || (k >= 37 && k <= 40))
    { }
    else if (k == 13) {
        window.event.keyCode = 9;
    }
    else {
        window.event.returnValue = false;
    }
}

//限制输入邮箱  示例：<input type="text" id="txtEmail" onblur="checkemail(this);" />
function checkemail(obj) {
    var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
    if (!sReg.test(obj.value)) {
        alert("请输入正确的邮箱!");

        obj.value = "";
    }
}


//限制只能输入数字  示例：<input type="text" id="txtNumber" onkeydown="numberKeyDown(this);">
function numberKeyDown(o) {
    var k = window.event.keyCode;

    if ((k == 46) || (k == 8) || (k >= 48 && k <= 57) || (k >= 96 && k <= 105) || (k >= 37 && k <= 40)) {
        if (o.value != '') {
            if (o.value.indexOf('0') == 0) {
                o.value = '';
            }
        }
    }
    else if (k == 13) {
        window.event.keyCode = 9;
    }
    else {
        window.event.returnValue = false;
    }
}

function printPage() {
    window.print();
}

// 弹出窗口，并且居中显示  url:弹出窗口页面路径  width:弹出窗口宽度   height:弹出窗口高度
function openForm(url, width, height) {
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var parms = "width=" + width + ", height=" + height + ", top=" + top + ", left=" + left;
    parms += ", toolbar=no, scrollbars=yes, menubar=no, location=no, resizable=yes";
    var frm = window.open(url, "openform", parms);
    frm.focus();
}

// 移除表格所有数据行
function clearTableRows(tb) {
    while (tb.rows.length > 1) {
        tb.deleteRow(1);
    }
}

// 删除表格的其中一行
function deleteTableRow(tr, tableID) {
    var tb = document.getElementById(tableID);
    tb.deleteRow(tr.rowIndex);
}

// 表格行移动方法
var tableSortClass = function () {
    //使表格行上移，接收参数为链接对象   
    this.moveUp = function (row) {
        //如果不是第一行，则与上一行交换顺序   
        if (row.rowIndex > 1) swapNode(row, row.previousSibling);
    }

    //使表格行下移，接收参数为链接对象   
    this.moveDown = function (row) {
        //如果不是最后一行，则与下一行交换顺序   
        if (row.nextSibling) swapNode(row, row.nextSibling);
    }
    //定义通用的函数交换两个结点的位置   
    function swapNode(row1, row2) {
        //获取父结点
        var _parent = row1.parentNode;
        //获取两个结点的相对位置
        var _t1 = row1.nextSibling;
        var _t2 = row2.nextSibling;
        //将row2插入到原来node1的位置
        if (_t1) _parent.insertBefore(row2, _t1);
        else _parent.appendChild(row2);
        //将row1插入到原来node2的位置
        if (_t2) _parent.insertBefore(row1, _t2);
        else _parent.appendChild(row1);
    }
}


// 移动下拉框被选中选项，将A下拉框的选项移动B下拉框
// fromSelId : 源下拉框id
// toSelId   : 目标下拉框id
function moveOption(fromSelId, toSelId) {
    var fromSel = document.getElementById(fromSelId);
    var toSel = document.getElementById(toSelId);

    if (fromSel == null || toSel == null) {
        return false;
    }

    for (var i = 0; i < fromSel.options.length; i++) {
        var _option = fromSel.options[i];
        // 是否选中
        if (_option.selected) {
            // 往目标下拉框加添加选项
            _option.selected = false;
            toSel.appendChild(_option);
            i--; // 下标变了
        }
    }
}